#include "src/main.h"

static const char *getenv_default(const char *name, const char *dflt)
{
    return getenv(name) ?: dflt;
}

#if LV_USE_LINUX_FBDEV
static void lv_linux_disp_init(void)
{
    const char *device = getenv_default("LV_LINUX_FBDEV_DEVICE", "/dev/fb0");
    lv_display_t *disp = lv_linux_fbdev_create();

    lv_linux_fbdev_set_file(disp, device);
}
#elif LV_USE_LINUX_DRM
static void lv_linux_disp_init(void)
{
    const char *device = getenv_default("LV_LINUX_DRM_CARD", "/dev/dri/card0");
    lv_display_t *disp = lv_linux_drm_create();

    lv_linux_drm_set_file(disp, device, -1);
}
#elif LV_USE_SDL
static void lv_linux_disp_init(void)
{
    const int width = atoi(getenv("LV_SDL_VIDEO_WIDTH") ?: "800");
    const int height = atoi(getenv("LV_SDL_VIDEO_HEIGHT") ?: "480");

    lv_sdl_window_create(width, height);
}
#else
#error Unsupported configuration
#endif

//-------------------------------------------------------------------
// 全局变量
//-------------------------------------------------------------------

int16_t currentScreen = -1;

int main(void)
{
    lv_init();
    /*Linux display device init*/
    lv_linux_disp_init();

    printf("init input\n");

// 切换运行平台（gec6818、模拟器）
#if LV_USE_LINUX_FBDEV
    lv_indev_t *touch_indev = lv_evdev_create(LV_INDEV_TYPE_POINTER, "/dev/input/event0");
    lv_evdev_set_calibration(touch_indev, 0, 0, 1024, 600);
#elif LV_USE_SDL
    // init input device
    lv_sdl_mouse_create();
    lv_sdl_keyboard_create();
    lv_sdl_mousewheel_create();
#endif

    printf("init input  end\n");

    load_system_date(); // 加载系统链表

    ui_init(); // 调用ui界面
    my_ui_init(); //自己二次修改界面
    ui_vars_init(); // 初始化ui变量更新模块

    /*Handle LVGL tasks*/
    while (1)
    {
        lv_timer_handler();
        usleep(5000);
    }
    return 0;
}
